package com.digitalgeko.hourreporter.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public final class UserTableManager {

	public static long createUser(String userName, String password, Context context){
		DataBaseManager manager = new DataBaseManager(context);
		SQLiteDatabase db = manager.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		cv.put(DataBaseManager.USER_FIELD_USER, userName);
		cv.put(DataBaseManager.USER_FIELD_PASSWORD, password);
		
		long result = db.insert(DataBaseManager.USER_TABLE, null, cv);
		
		db.close();
		manager.close();
		
		return result;
	}
	
	public static Integer validateUser(String userName, String password, Context context){
		Integer result = -1;
		
		DataBaseManager manager = new DataBaseManager(context);
		SQLiteDatabase db = manager.getReadableDatabase();
		
		Cursor c = db.query(DataBaseManager.USER_TABLE, new String[]{DataBaseManager.USER_FIELD_ID}, 
				DataBaseManager.USER_FIELD_USER +  "=? and " + DataBaseManager.USER_FIELD_PASSWORD + "=?", 
				new String[]{userName, password}, null, null, null);
		
		if(c.moveToFirst()){
			result = c.getInt(0);
		}
		
		c.close();
		db.close();
		manager.close();
		
		return result;
	}
	
}
